import create from 'dd-store'
import pageStore from '../../stores/globalStore'
import { getStaffPerformanceListApi, selectMyPmById } from '../../api/manage'
import { getUserInfoByUserDDId } from "../../api/index"


create.Page({
  store: pageStore,
  data: {
    // 员工ID
    userDDId: '',
    // 员工名称
    userName: '',
    // 员工绩效列表
    staffPerformanceList: []
  },
  onLoad(options) {
    this.setData({
      userDDId: options.userDDId,
      userName: options.userName
    })
    this.getStaffPerformanceListFunc()
  },

  // 绩效点击事件
  perItemClick(e) {
    let eItem = e.target.dataset.info
    console.log(eItem);

    // 判断绩效状态
    if (eItem.status == 0) {
      dd.showToast({
        content: "绩效已撤销"
      })
      return
    }

    let gData1 = {
      pmId: eItem.pmId
    }
    let gData2 = {
      userDDId: ''
    }
    let navParamData = {
      pmId: eItem.pmId,
      userName: "",
      avatar: "",
      pmDeptName: ""
    }

    // 根据pmId查询绩效信息
    selectMyPmById(gData1).then((res) => {
      console.log('查询绩效信息', res);
      gData2.userDDId = res.data.pmUserDDId
      navParamData.pmDeptName = res.data.pmDeptName

      // 根据该绩效的归属者userDDId查询用户信息
      getUserInfoByUserDDId(gData2).then((res1) => {
        console.log('该绩效的用户信息', res1);
        navParamData.userName = res1.data.userName
        navParamData.avatar = res1.data.avatar

        // 进行跳转页面
        dd.navigateTo({
          url: `/pages/usual/performancedetail/performancedetail?pmid=${navParamData.pmId}&userName=${navParamData.userName
            }&avatar=${navParamData.avatar}&dept=${navParamData.pmDeptName}&userId=${res1.data.userId}&managerUserId=${res1.data.managerUserDDId}`,
        })
      })
    })
  },
  // 查询员工绩效列表函数
  async getStaffPerformanceListFunc() {
    dd.showLoading({
      content: "加载中"
    })
    let data = {
      userDDId: this.data.userDDId
    }
    await getStaffPerformanceListApi(data).then((res) => {
      console.log('员工绩效列表', res);
      dd.hideLoading()
      this.setData(({
        staffPerformanceList: res.data
      }))
    }).catch(() => {
      dd.hideLoading()
    })
  }
});
